package com.example.xiaoshuo_yudu.service.impl;

import com.example.xiaoshuo_yudu.entity.entity.News;
import com.example.xiaoshuo_yudu.mapper.NewsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xiaoshuo_yudu.service.NewsService;
import com.example.xiaoshuo_yudu.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 新闻信息表 服务实现类
 * </p>
 *
 * @author xusen
 * @since 2025-07-24
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<News> get() {
        String newsSelect = Const.NEWS_SELECT;
        if (redisTemplate.opsForValue().get(newsSelect) != null) {
            return (List<News>) redisTemplate.opsForValue().get(newsSelect);
        }
        List<News> news = baseMapper.selectList(null);
        redisTemplate.opsForValue().set(newsSelect, news,100, TimeUnit.DAYS);
        return  news ;
    }
}
